package com.tiennt.android.apkextractor.utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.util.Log;

public class FileUtils {
	/**
	 * All paths must end with slash "/"
	 * 
	 * @param fromPath
	 * @param fileName
	 * @param toPath
	 * @return
	 */
	public static boolean copyFile(String fromPath, String fileName,
			String toPath) {
		InputStream in = null;
		OutputStream out = null;
		try {

			// create output directory if it doesn't exist
			File dir = new File(toPath);
			if (!dir.exists()) {
				dir.mkdirs();
			}

			in = new FileInputStream(fromPath + fileName);
			out = new FileOutputStream(toPath + fileName);

			byte[] buffer = new byte[1024];
			int read;
			while ((read = in.read(buffer)) != -1) {
				out.write(buffer, 0, read);
			}
			in.close();
			in = null;

			// write the output file (You have now copied the file)
			out.flush();
			out.close();
			out = null;

			return true;
		} catch (FileNotFoundException fnfe1) {
			Log.e(Config.TAG, fnfe1.getMessage());
		} catch (Exception e) {
			Log.e(Config.TAG, e.getMessage());
		}
		return false;
	}

}
